﻿using Com.Lancetinc.Evs.Helpers;

namespace Com.Lancetinc.Evs.ViewModels
{
    public class AppStatusViewModel : ViewModelBase
    {
        private string _instrumentState = "OFF";
        public string InstrumentState
        {
            get => _instrumentState;
            set => SetProperty(ref _instrumentState, value);
        }

        /// <summary>
        /// 序列号是否可以见？
        /// </summary>
        public bool IsHcSerialNumberVisible { get; }

        private string _hcSerialNumber;

        /// <summary>
        /// 耗材序列号
        /// </summary>
        public string HcSerialNumber
        {
            get => _hcSerialNumber;
            set => SetProperty(ref _hcSerialNumber, value);
        }

        private bool _isShowValidUntil;
        public bool IsShowValidUntil
        {
            get => _isShowValidUntil;
            set => SetProperty(ref _isShowValidUntil, value);
        }

        private string _validUntil;
        public string ValidUntil
        {
            get => _validUntil;
            set => SetProperty(ref _validUntil, value);
        }

        private int _hcUsedTime;
        /// <summary>
        /// 模块运行时间
        /// </summary>
        public int HcUsedTime
        {
            get => _hcUsedTime;
            set
            {
                SetProperty(ref _hcUsedTime, value);
                RaisePropertyChanged(nameof(HcUsedTimeDisp));
            }
        }

        public string HcUsedTimeDisp => HcUsedTime == 0 ? string.Empty : HcUsedTime.ToString();

        private string _ledState;
        public string LedState
        {
            get => _ledState;
            set => SetProperty(ref _ledState, value);
        }

        private string _errorMessage;
        public string ErrorMessage
        {
            get => _errorMessage;
            set
            {
                SetProperty(ref _errorMessage, value);
                if (!string.IsNullOrEmpty(value))
                {
                    WpfHelper.ExecLaterEx("dismissErrorMsg", () =>
                    {
                        ErrorMessage = null;
                    }, 5 * 60_000);
                }
            }
        }

        public AppStatusViewModel()
        {
            // 在config.ini配置文件中若没有e=1，则默认不显示内窥镜有效期
            IsShowValidUntil = "e=1".Equals(AppConfigHelper.GetFirstConfigLine("e="));
            IsHcSerialNumberVisible = "s=1".Equals(AppConfigHelper.GetFirstConfigLine("s="));
            RegisterEvents();
        }

        private void RegisterEvents()
        {
            ActiveApp.Instance.InstManager.StateChanged += InstManager_StateChanged;
        }

        private void InstManager_StateChanged(object sender, Communications.InstrumentInfoChangedEventArgs e)
        {
            var status = this;
            status.InstrumentState = e.Instrument.InstrumentConnected ? "ON" : "OFF";
            if (!string.IsNullOrEmpty(e.Instrument.HcSerialNo))
            {
                var validUtilMonths = AppConfigHelper.GetShelfLifeMonthsByHcSerialNo(e.Instrument.HcSerialNo);
                status.HcSerialNumber = e.Instrument.HcSerialNo;
                status.HcUsedTime = e.Instrument.HcUsedTime;
                status.ValidUntil = e.Instrument.FactoryTime.AddMonths(validUtilMonths).ToShortDateString();
            }
            else
            {
                status.HcSerialNumber = string.Empty;
                status.HcUsedTime = 0;
                status.ValidUntil = string.Empty;
            }

            status.LedState = e.Instrument.LedBrightness > 0 ? e.Instrument.LedBrightness.ToString() : "OFF";

            if (e.ChangedPropertyNames.Contains(PropertyConsts.ErrorCode))
            {
                var tag = "ins_error";
                if (e.Instrument.ErrorCode > 0)
                {
                    var errMsg = InstrumentErrorHelper.GetErrorMessage(e.Instrument);
                    if (errMsg != null)
                    {
                        ActiveApp.Instance.MainWindowVm.ShowPinnedError(errMsg, tag: tag);
                    }
                }
                else
                {
                    ActiveApp.Instance.MainWindowVm.ClearPinnedError(null, tag);
                }
            }
        }
    }
}
